﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Utility
{
    /// <summary>
    /// 封装了post请求的一些参数和用法
    /// </summary>
    public abstract class QuickPostBase
    {

        public QuickPostBase()
        {
            this.param = new Dictionary<string, object>();
        }
        /// <summary>
        /// 访问的控制器和方法
        /// </summary>
        protected string ControllerAction { get; set; }

        /// <summary>
        /// 接口公共部分
        /// </summary>
        protected  string BaseUrl { get; set; } = "http://localhost:8701/api/";
        /// <summary>
        /// json参数
        /// </summary>
        protected Dictionary<string,object> param { get; set; } = null;


        /// <summary>
        /// 访问是否需要验证才能请求成功，默认是true
        /// </summary>
      //  protected bool useAuth { get { } }

        private bool useAuth;

        public bool UseAuth
        {
            get { return useAuth; }
            set { useAuth = value;this._setParam(value); }
        }

        private void _setParam(bool auth)
        {
            if (auth)
            {
                this.param = new Dictionary<string, object>();
                // todo:这里要加上token验证信息
            }
            else
            {
                this.param = new Dictionary<string, object>();
            }
        }

        /// <summary>
        ///post请求
        /// </summary>
        /// <returns></returns>
        protected string PostData()
        {
            string url = BaseUrl + this.ControllerAction;
            try
            {
                if (param.Count==0)
                {
                    return CommonHelper.HttpPost(url, this.useAuth);
                }
                else
                {
                    return CommonHelper.HttpPost(url, this.param, this.useAuth);
                }

            }
            catch (Exception e)
            {

                return ErrorLevel.失败.ToString();
            }
        }

        /// <summary>
        /// 写入log文件
        /// </summary>
        /// <param name="msg"></param>
        protected void WriteLog(string msg)
        {
            LogHelper.WriteLog(msg);
        }

       
    }
}
